package pl.put.miasi.volleyball;


import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundThread implements Runnable {
	Thread thread;
	String fileName = "";
	boolean loaded = false;
	int sound = 0;
	
    private static final String WALKING_WAV_PATH = "dzwieki\\walking.wav"; 
    private static final String RETURNING_WAV_PATH = "dzwieki\\returning.wav"; 
    private static final String NEWROUND_WAV_PATH = "dzwieki\\newround.wav"; 
	
    public static final int WALKING = 0;
    public static final int RETURNING = 1;
    public static final int NEWROUND = 2;
    
    private AudioInputStream walkingAIS = null;
    private AudioInputStream returningAIS = null;
    private AudioInputStream newRoundAIS = null;
    
	private SoundThread()
	{
		
	}
	
	private SoundThread(String fileName, String threadName)
	{
		thread = new Thread(this, threadName);
		thread.start();
		this.fileName = fileName;
	}

	public SoundThread(int sound) {
		thread = new Thread(this, "");
		this.sound = sound;
		thread.start();
	}

	@Override
	public void run() {
		//playSound(sound);
	}
	
	public void playSound(int sound) {
		if (!loaded) {
			load();
			loaded = true;
		}
			
		AudioInputStream ais = null;
		switch (sound) {
			case WALKING:
				ais = walkingAIS;
				break;
			case NEWROUND:
				ais = newRoundAIS;
				break;
			case RETURNING:
				ais = returningAIS;
				break;
		}
		
		AudioFormat format = ais.getFormat();
		SourceDataLine dataLine = null;
		DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
		try {
			dataLine = (SourceDataLine)AudioSystem.getLine(info);
			dataLine.open(format);
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
		
		if (dataLine.isControlSupported(FloatControl.Type.PAN))
		{
			FloatControl pan = (FloatControl)dataLine.getControl(FloatControl.Type.PAN);
			pan.setValue(0.0f); 
		}
		dataLine.start();
		int bytes = 0;
		byte[] data = new byte[524288];
		try
		{
			while (bytes != -1)
			{
				bytes = ais.read(data, 0, data.length);
				if (bytes >= 0)
					dataLine.write(data, 0, bytes);
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			dataLine.drain();
			dataLine.close();
		}
	}
	
	public void load() {
		File soundFile = new File(WALKING_WAV_PATH);
    	if (soundFile.exists())
    	{
    		try {
				walkingAIS = AudioSystem.getAudioInputStream(soundFile);
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}    			
    	}
		soundFile = new File(NEWROUND_WAV_PATH);
    	if (soundFile.exists())
    	{
    		try {
				newRoundAIS = AudioSystem.getAudioInputStream(soundFile);
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}    			
    	}
		soundFile = new File(RETURNING_WAV_PATH);
    	if (soundFile.exists())
    	{
    		try {
				returningAIS = AudioSystem.getAudioInputStream(soundFile);
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}    			
    	}
	}
	
    public void load(String fileName)
    {
    	File soundFile = new File(fileName);
    	if (soundFile.exists())
    	{
    		AudioInputStream stream = null;
    		try {
				stream = AudioSystem.getAudioInputStream(soundFile);
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}    			
			
			AudioFormat format = stream.getFormat();
			SourceDataLine dataLine = null;
			DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
			try {
				dataLine = (SourceDataLine)AudioSystem.getLine(info);
				dataLine.open(format);
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			}
			
			if (dataLine.isControlSupported(FloatControl.Type.PAN))
			{
				FloatControl pan = (FloatControl)dataLine.getControl(FloatControl.Type.PAN);
				pan.setValue(0.0f); 
			}
			dataLine.start();
			int bytes = 0;
			byte[] data = new byte[524288];
			try
			{
				while (bytes != -1)
				{
					bytes = stream.read(data, 0, data.length);
					if (bytes >= 0)
						dataLine.write(data, 0, bytes);
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			finally
			{
				dataLine.drain();
				dataLine.close();
			}
    	}
    }    
}
